package com.googlecode.spoluyan.google.googlefeed;

import java.util.HashMap;
import java.util.Map;

import com.googlecode.spoluyan.google.common.AbstractGoogleAPIClient;
import com.googlecode.spoluyan.google.common.Defines;
import com.googlecode.spoluyan.google.common.Utils;
import com.googlecode.spoluyan.google.googlefeed.data.FindFeedResponseData;
import com.googlecode.spoluyan.google.googlefeed.data.LoadFeedResponseData;

public class GoogleFeedClient extends AbstractGoogleAPIClient {

    public GoogleFeedClient(String key) {
	super(key);
	this.name = Defines.ClientName.GOOGLE_FEED;
    }

    public FindFeedResponseData findFeed(String query) {
	if (Utils.isEmptyString(query)) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	queryParams.put(Defines.GoogleFeedDefines.Parameter.QUERY, query);
	queryParams.put(Defines.GoogleFeedDefines.Parameter.VERSION, Defines.GoogleFeedDefines.VERSION_VALUE);
	return Utils.fromJSON(
		Utils.doRequest(queryParams, Defines.GoogleFeedDefines.API_URL, Defines.GoogleFeedDefines.Path.FIND),
		FindFeedResponseData.class);
    }

    public LoadFeedResponseData loadFeed(String query) {
	if (Utils.isEmptyString(query)) {
	    return null;
	}
	Map<String, String> queryParams = new HashMap<String, String>();
	queryParams.put(Defines.GoogleFeedDefines.Parameter.QUERY, query);
	queryParams.put(Defines.GoogleFeedDefines.Parameter.VERSION, Defines.GoogleFeedDefines.VERSION_VALUE);
	return Utils.fromJSON(
		Utils.doRequest(queryParams, Defines.GoogleFeedDefines.API_URL, Defines.GoogleFeedDefines.Path.LOAD),
		LoadFeedResponseData.class);
    }
}
